﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Task.Logic.Interface;
using App.Service;
using Task.Entity;
using Ultimus.WFServer;
using App.Integration;
namespace Task
{
    public partial class NewTask : System.Web.UI.Page
    {
        IProcessCategory _pc = ServiceContainer.GetService<IProcessCategory>();
        List<ProcessEntity> _initProcessList = new List<ProcessEntity>();
        Task.Logic.Interface.ITask _task = ServiceContainer.GetService<Task.Logic.Interface.ITask>();
        protected void Page_Load(object sender, EventArgs e)
        {
            //load init process
            _initProcessList = _task.GetProcessList(SSOUtil.Context.GetLoginedUser().UserAccount);

            //bind category
            List<ProcessCategoryEntity> lists = _pc.GetList();
            ProcessCategoryEntity pe = new ProcessCategoryEntity();
            pe.CategoryName = Task.Content.lang.NewTask_AllProcess;
            lists.Insert(0, pe);
            rpProcessCategory.DataSource = lists;
            rpProcessCategory.DataBind();

            rpAllProcess.DataSource = lists;
            rpAllProcess.DataBind();

            txtCurrentCategory.Text = pe.CategoryID;
        }

        protected void rpAllProcess_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                //load process by category
                Repeater rpControl = (Repeater)e.Item.FindControl("rpProcess");
                ProcessCategoryEntity pce = (ProcessCategoryEntity)e.Item.DataItem;
                List<ProcessEntity> lists = new List<ProcessEntity>();
                if (pce.CategoryName == Task.Content.lang.NewTask_AllProcess)
                {
                    lists = _pc.GetProcessList(null);
                }
                else
                {
                    lists = _pc.GetProcessList(pce.CategoryName);
                }
                lists = FilterList(lists);
                rpControl.DataSource = lists;
                rpControl.DataBind();

            } 
        }

        List<ProcessEntity> FilterList(List<ProcessEntity> lists)
        {
            List<ProcessEntity> newlist = new List<ProcessEntity>();
            for (int i = 0; i < _initProcessList.Count; i++)
            {
                ProcessEntity process= lists.Find(delegate(ProcessEntity entity) {
                    if (entity.ProcessName.ToUpper().Trim() == _initProcessList[i].ProcessName.ToUpper().Trim())
                    {
                        return true;
                    }
                    return false;
                });
                if (process != null)
                {
                    newlist.Add(process);
                }
            }
            return newlist;
        }

        public string GetCategoryImage(string categoryName)
        {
            return categoryName == Task.Content.lang.NewTask_AllProcess ? "lc1.jpg" : "lc5.jpg";
        }
    }
}